package com.chao.storge.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chao.storge.entity.StorgeInfo;
import com.chao.storge.mapper.StorgeInfoMapper;
import com.chao.storge.service.StorgeInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author : Rookie
 * @description :
 * @date : 2022/9/18
 */
@Service
public class StorgeInfoServiceImpl extends ServiceImpl<StorgeInfoMapper, StorgeInfo> implements StorgeInfoService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deductStorge(String productId, Integer count) {
        StorgeInfo storgeInfo = baseMapper.selectOne(Wrappers.<StorgeInfo>lambdaQuery().eq(StorgeInfo::getProductId, productId));
        storgeInfo.setUsedStorge(storgeInfo.getUsedStorge() + count).setRemainStorge(storgeInfo.getRemainStorge() - count);
        return baseMapper.updateById(storgeInfo) > 0;
    }
}
